package org.study.mybatis.session;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.study.mybatis.binding.MapperMethod;
import org.study.mybatis.binding.MapperRegistry;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 读取配置文件
 */
public class Configuration {
    //流
    private InputStream inputStream;
    MapperRegistry mapperRegistry = new MapperRegistry();

    //加载配置文件
    public void loadConfiguration() throws DocumentException, IOException {
        //使用dom4j工具包解析
        Document document = new SAXReader().read(inputStream);
        //获取root节点
        Element root = document.getRootElement();
        //获取配置文件中的mapper文件存放的位置,可以配置多个不同的路径
        List<Element> mappers = root.element("mappers").elements("mapper");
        for (Element element : mappers) {
            //定位到mapper存放的具体地方
            if (element.attribute("resource") != null) {
                //获取到具体的mapper
                String mapperPath = element.attribute("resource").getText();
                //通过mapper路径找到xml，并且解析xml返回一个map
                Map map = loadXMLConfiguration(mapperPath);
                //设置mapperRegistry的容器里面的map
                mapperRegistry.setKnownMappers(map);
            }
        }
    }

    //解析mapper文件具体内容
    public Map loadXMLConfiguration(String path) throws IOException {
        Map map = new HashMap();
        InputStream in = null;
        try {
            //获取sql文件的流
            in = this.getClass().getClassLoader().getResourceAsStream(path);
            Document document = new SAXReader().read(in);
            Element root = document.getRootElement();
            if (root.getName().equalsIgnoreCase("mapper")) {
                //获取mapper文件中的namespace,
                String namespace = root.attribute("namespace").getText();
                //这里
                for (Element select : (List<Element>) root.elements("select")) {
                    //构建一个mapperMethod对象
                    MapperMethod mapperMethod = new MapperMethod();
                    //执行sql
                    mapperMethod.setSql(select.getTextTrim());
                    //入参类型
                    mapperMethod.setType(Class.forName(select.attribute("resultType").getText()));
                    //
                    map.put(namespace + "." + select.attribute("id").getText(), mapperMethod);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            in.close();

        }

        return map;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public MapperRegistry getMapperRegistry() {
        return mapperRegistry;
    }

    public void setMapperRegistry(MapperRegistry mapperRegistry) {
        this.mapperRegistry = mapperRegistry;
    }
}
